﻿using EVEBattery.DB;
using EVEBatteryCheck;
using EVEPreChargeDataMonitor.Data;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using TYBase;

namespace EVEPreChargeDataMonitor
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private ConcurrentQueue<FileInfo> queueFile = new ConcurrentQueue<FileInfo>();
        public MainWindow()
        {
            InitializeComponent();
            this.Loaded += MainWindow_Loaded;
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            StartSaveToDB();
            initTimer();
        }
        Thread thread = null;
        AutoResetEvent resetEvent = new AutoResetEvent(false);

        private void StartSaveToDB()
        {
            thread = new Thread(() =>
            {
                if (queueFile.TryDequeue(out FileInfo fileName))
                {
                    File2Data(fileName);
                }
            })
            { IsBackground = true };
            thread.Start();
        }
        TxtDataHelper dataHelper = new TxtDataHelper();
        private void File2Data(FileInfo fileName)
        {
            dataHelper.FileToData(fileName.FullName, out var tray);

            using (var dx = DBHelper.CreateConnect())
            {
                foreach (var cell in tray.Cells)
                {
                    dx.Insert(new tbl_charge_data() { cell_no = cell.Value, create_time = DateTime.Now, lotId = tray.LotId, tray_code = tray.TrayName, type_id = tray.TypeId });
                }

            }
        }

        private System.Threading.Timer timerClose;

        private void initTimer()
        {
            timerClose = new System.Threading.Timer(timerCall, null, 1000, 1000 * 60);
        }
        bool isChecking = false;
        private void timerCall(object state)
        {
            if (queueFile.Count == 0 && isChecking == false)
            {
                isChecking = true;
                DateTime dt = AppIniData.LastCheckTime;
                if (dt < DateTime.Now.AddMonths(-1))
                {
                    dt = DateTime.Now.AddMonths(-1);
                }
                DirectoryInfo info = new DirectoryInfo(AppIniData.CheckDir);
                Thread.Sleep(2000);
                var files = info.GetFiles().Where(x => x.LastWriteTime > dt && x.LastWriteTime <= DateTime.Now.AddSeconds(-60)).OrderBy(p => p.LastWriteTime);
                //这里加了个延迟处理
                foreach (var item in files)
                {
                    queueFile.Enqueue(item);
                    if (item.LastAccessTime > dt)
                    {
                        dt = item.LastAccessTime;
                    }
                }
                AppIniData.LastCheckTime = dt;
                AppData.ini.WriteString(nameof(AppIniData.LastCheckTime), AppIniData.LastCheckTime.AsString());
                resetEvent.Set();
                isChecking = false;
            }
        }
    }
}
